﻿using System;
using System.Collections.Generic;
//using System.Drawing;
using System.Xml;
using System.Xml.Linq;

namespace Plot08
{
    class XmlTool
    {

        static string _xmlFileName = "";
        /// <summary>
        /// 有参构造函数
        /// </summary>
        /// <param name="xmlFileName"></param>
        public XmlTool(string xmlFileName)
        {
            _xmlFileName = xmlFileName;
        }

        #region 属性定义

        private string gc_nm = "";

        public string TK_GC_NM
        {

            get
            {

                string xPath = "/Root/TkInfo/工程名称[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    gc_nm = tmpValue;

                }
                else
                {
                    gc_nm = "默认工程名称";

                }

                return gc_nm;
            }
            set
            {

                gc_nm = value;

                string xPath = "/Root/TkInfo/工程名称[1]";

                ModifyElemnt(xPath, gc_nm.ToString());

            }

        }

        private string xm_nm = "";

        public string TK_XM_NM
        {

            get
            {

                string xPath = "/Root/TkInfo/项目名称[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    xm_nm = tmpValue;

                }
                else
                {

                    xm_nm = "默认项目名称";
                }

                return xm_nm;
            }
            set
            {

                xm_nm = value;

                string xPath = "/Root/TkInfo/项目名称[1]";

                ModifyElemnt(xPath, xm_nm.ToString());

            }

        }

        private string tz_nm = "";

        public string TK_TZ_NM
        {

            get
            {

                string xPath = "/Root/TkInfo/图纸名称[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    tz_nm = tmpValue;

                }
                else
                {

                    tz_nm = "默认图纸名称";
                }




                return tz_nm;
            }
            set
            {

                tz_nm = value;

                string xPath = "/Root/TkInfo/图纸名称[1]";

                ModifyElemnt(xPath, tz_nm.ToString());

            }

        }

        private string tz_nm_A = "";

        public string TK_TZ_NM_A
        {

            get
            {

                string xPath = "/Root/TkInfo/图纸名称1[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    tz_nm_A = tmpValue;

                }
                else
                {

                    tz_nm_A = "";
                }


                return tz_nm_A;
            }
            set
            {

                tz_nm_A = value;

                string xPath = "/Root/TkInfo/图纸名称1[1]";

                ModifyElemnt(xPath, tz_nm_A.ToString());

            }

        }

        private string tz_bh = "";

        public string TK_TZ_BH
        {

            get
            {

                string xPath = "/Root/TkInfo/图纸编号[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    tz_bh = tmpValue;

                }
                else
                {

                    tz_bh = "默认图纸编号";

                }

                return tz_bh;
            }
            set
            {

                tz_bh = value;

                string xPath = "/Root/TkInfo/图纸编号[1]";

                ModifyElemnt(xPath, tz_bh.ToString());

            }

        }

        private string tz_bb = "";

        public string TK_TZ_BB
        {

            get
            {

                string xPath = "/Root/TkInfo/版本[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    tz_bb = tmpValue;

                }
                else
                {

                    tz_bb = "默认版本";

                }


                return tz_bb;
            }
            set
            {

                tz_bb = value;

                string xPath = "/Root/TkInfo/版本[1]";

                ModifyElemnt(xPath, tz_bb.ToString());

            }

        }

        private string tz_gh = "";

        public string TK_TZ_GH
        {

            get
            {

                string xPath = "/Root/TkInfo/工号[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    tz_gh = tmpValue;

                }
                else
                {

                    tz_gh = "默认版本";
                }



                return tz_gh;
            }
            set
            {

                tz_gh = value;

                string xPath = "/Root/TkInfo/工号[1]";

                ModifyElemnt(xPath, tz_gh.ToString());

            }

        }

        private string tz_jd = "";

        public string TK_TZ_JD
        {

            get
            {

                string xPath = "/Root/TkInfo/阶段[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    tz_jd = tmpValue;

                }
                else
                {

                    tz_jd = "默认版本";

                }

                return tz_jd;
            }
            set
            {

                tz_jd = value;

                string xPath = "/Root/TkInfo/阶段[1]";

                ModifyElemnt(xPath, tz_jd.ToString());

            }

        }

        private string tz_zy = "";

        public string TK_TZ_ZY
        {

            get
            {

                string xPath = "/Root/TkInfo/专业[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    tz_zy = tmpValue;

                }
                else
                {

                    tz_zy = "默认版本";

                }

                return tz_zy;

            }
            set
            {

                tz_zy = value;

                string xPath = "/Root/TkInfo/专业[1]";

                ModifyElemnt(xPath, tz_zy.ToString());

            }

        }

        private string tz_rq = "";

        public string TK_TZ_RQ
        {

            get
            {

                string xPath = "/Root/TkInfo/日期[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    tz_rq = tmpValue;

                }
                else
                {

                    tz_rq = "默认版本";

                }

                return tz_rq;
            }
            set
            {

                tz_rq = value;

                string xPath = "/Root/TkInfo/日期[1]";

                ModifyElemnt(xPath, tz_rq.ToString());

            }

        }

        private int tz_dy = 1;

        public int TK_TZ_DY
        {

            get
            {

                string xPath = "/Root/TkInfo/第页[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    try
                    {

                        tz_dy = Convert.ToInt16(tmpValue);

                    }
                    catch (System.Exception)
                    {

                        //						throw;

                        tz_dy = 1;
                    }

                }
                else
                {

                    tz_dy = 1;

                }

                return tz_dy;

            }

            set
            {

                if (value > 0)
                {

                    tz_dy = value;

                }
                else
                {

                    tz_dy = 1;

                }

                string xPath = "/Root/TkInfo/第页[1]";

                ModifyElemnt(xPath, tz_rq.ToString());


            }

        }

        private int tz_zongye = 1;

        public int TK_TZ_PAGES
        {

            get
            {

                string xPath = "/Root/TkInfo/总页[1]";

                string tmpValue = GetXmlElementValue(xPath);

                if (tmpValue != null)
                {

                    try
                    {

                        tz_zongye = Convert.ToInt16(tmpValue);

                    }
                    catch (System.Exception)
                    {

                        //						throw;

                        tz_zongye = 1;
                    }

                }
                else
                {

                    tz_zongye = 1;

                }

                return tz_zongye;

            }

            set
            {

                if (value > 0)
                {

                    tz_zongye = value;

                }
                else
                {

                    tz_zongye = 1;

                }



                string xPath = "/Root/TkInfo/总页[1]";

                ModifyElemnt(xPath, tz_rq.ToString());


            }

        }


        #endregion

        #region 与操作图框相关的xml处理，删除对本程序应该没有影响，暂时保留


    /// <summary>
    /// 获取目录信息
    /// </summary>
    /// <returns></returns>

        public List<string> Get_MlInfo()
        {
            List<string> tmpList = new List<string>();

            tmpList.Clear();

            string xPath = "/Root/MuluInfo/图纸";

            XmlDocument doc = new XmlDocument();

            doc.Load(_xmlFileName);    //加载Xml文件 

            XmlElement root = doc.DocumentElement;   //获取根节点 

            XmlNodeList nodeList = root.SelectNodes(xPath);

            if (nodeList != null)
            {

                foreach (XmlNode element in nodeList)
                {

                    tmpList.Add(element.InnerText);
                }

            }

            return tmpList;

        }

        /// <summary>
        /// 删除xml文件中目录信息
        /// </summary>
        public void Del_MlInfo()
        {

            string xPath = "/Root/MuluInfo[1]";

            XmlDocument doc = new XmlDocument();

            doc.Load(_xmlFileName);    //加载Xml文件 

            XmlElement root = doc.DocumentElement;   //获取根节点 

            XmlNode node = root.SelectSingleNode(xPath);

            if (node != null)
            {

                node.RemoveAll();

            }

            doc.Save(_xmlFileName);

        }


    /// <summary>
        /// 根据提供的字符串创建目录文件，文件中必须有Root节点
    /// </summary>
    /// <param name="tmpStrs">目录信息字符串集合，每个字符串应包含图名图号信息</param>
        public void Create_MlInfo(List<string> tmpStrs)
        {

            XDocument doc = XDocument.Load(_xmlFileName);

            XElement muLuXelement = doc.Root.Element("MuluInfo");

            if (muLuXelement != null)
            {

                muLuXelement.RemoveAll();

                foreach (string element in tmpStrs)
                {

                    XElement tmpXelement = new XElement("图纸", element);

                    muLuXelement.Add(tmpXelement);
                }

            }
            else
            {

                muLuXelement = new XElement("MuluInfo");

                foreach (string element in tmpStrs)
                {

                    XElement tmpXelement = new XElement("图纸", element);

                    muLuXelement.Add(tmpXelement);
                }

                doc.Root.Add(muLuXelement);

            }

            doc.Save(_xmlFileName);

        }


      

        /// <summary>
        ///  向文件中添加目录信息
        /// </summary>
        /// <param name="tmpStrs">目录信息字符串List，每个字符串应包含图名与图号信息</param>
        public void Add_MlInfo(List<string> tmpStrs)
        {

            XDocument doc = XDocument.Load(_xmlFileName);

            XElement muLuXelement = doc.Root.Element("MuluInfo");

            if (muLuXelement != null)
            {

                //muLuXelement.RemoveAll();

                foreach (string element in tmpStrs)
                {

                    XElement tmpXelement = new XElement("图纸", element);

                    muLuXelement.Add(tmpXelement);
                }

            }
            else
            {

                muLuXelement = new XElement("MuluInfo");

                foreach (string element in tmpStrs)
                {

                    XElement tmpXelement = new XElement("图纸", element);

                    muLuXelement.Add(tmpXelement);
                }

                doc.Root.Add(muLuXelement);

            }

            doc.Save(_xmlFileName);

        }

        /// <summary>
        /// 初始化话目录记录，需要有Root节点
        /// </summary>
        public void ChuShiHua_MlInfo()
        {
            XDocument doc = XDocument.Load(_xmlFileName);

            XElement muLuXelement = doc.Root.Element("MuluInfo");

            if (muLuXelement != null)
            {

                muLuXelement.RemoveAll();

                XElement tmpXelement = new XElement("图纸", "S-JT-01|设计说明");

                muLuXelement.Add(tmpXelement);

            }
            else
            {

                muLuXelement = new XElement("MuluInfo");

                XElement tmpXelement = new XElement("图纸", "S-JT-01|设计说明");

                muLuXelement.Add(tmpXelement);

                doc.Root.Add(muLuXelement);

            }

            doc.Save(_xmlFileName);

        }

     /// <summary>
     /// 初始化或者创建新的TkInfo
     /// </summary>
        public void CreateXmlContent_Tk()
        {

            XDocument xdocument = null;

            XNode rootNode = null;

            try
            {

                xdocument = XDocument.Load(_xmlFileName);

                rootNode = xdocument.Root;

            }
            catch (System.Exception)
            {

                //				throw;

                CreateXmlFile_Root();

            }

            xdocument = XDocument.Load(_xmlFileName);

            //			rootNode = xdocument.Root;

            XElement xe = xdocument.Root.Element("TkInfo");

            if (xe != null)
            {

                xe.Remove();

            }

            XElement Tkinfo = new XElement("TkInfo",

                                           new XElement("工程名称", "定江道(解放南路-韵海路)道路、排水、绿化工程"),

                                           new XElement("工程名称1", ""),

                                           new XElement("项目名称", "道路工程"),

                                           new XElement("图纸名称", "纵断面设计图"),

                                           new XElement("图纸编号", "S-L-01"),

                                           new XElement("版本", "A"),

                                           new XElement("工号", "17660L-S-JG"),

                                           new XElement("阶段", "初步设计"),

                                           new XElement("专业", "道路工程"),

                                           new XElement("日期", "2017.10"),

                                           new XElement("第页", "1"),

                                           new XElement("总页", "1")

                                       );


            XElement rootElemenmt = xdocument.Root;

            rootElemenmt.Add(Tkinfo);

            xdocument.Save(_xmlFileName);


        }

    
   /// <summary>
   /// 修改inertxt的内容
   /// </summary>
   /// <param name="xmlNodePath">给定的XPath路径</param>
   /// <param name="inerTextStr">将要修改为的记录信息</param>
        public void ModifyElemnt(string xmlNodePath, string inerTextStr)
        {

            XDocument xdocument = XDocument.Load(_xmlFileName);

            XElement root = xdocument.Root;

            if (root == null)
            {
                XmlTool xt = new XmlTool(_xmlFileName);

                xt.CreateXmlFile_Root();

            }

            xdocument = XDocument.Load(_xmlFileName);

            XElement xe = xdocument.Root.Element("TkInfo");

            if (xe == null)
            {
                XElement Tkinfo = new XElement("TkInfo");

                xdocument.Root.Add(Tkinfo);

                xdocument.Save(_xmlFileName);

            }

            XmlDocument doc = new XmlDocument();

            doc.Load(_xmlFileName);    //加载Xml文件 

            XmlElement root_tmp = doc.DocumentElement;   //获取根节点 

            XmlNode selectNode = root_tmp.SelectSingleNode(xmlNodePath);

            if (selectNode != null)
            {
                selectNode.InnerText = inerTextStr;

                doc.Save(_xmlFileName);

            }
            else
            {
                xdocument = XDocument.Load(_xmlFileName);

                xe = xdocument.Root.Element("TkInfo");

                string[] names = xmlNodePath.Trim('/').Split('/');

                string tmp_str = names[2].Remove(names[2].Length - 3, 3);

                XElement tmp_xmlE = new XElement(tmp_str, inerTextStr);

                xe.Add(tmp_xmlE);

                xdocument.Save(_xmlFileName);

            }

        }

        /// <summary>
        /// 获取指定路径的元素的值
        /// </summary>
        /// <param name="xmlNodePath"></param>
        /// <returns></returns>
        public string GetXmlElementValue(string xmlNodePath)
        {


            XmlDocument doc = new XmlDocument();

            doc.Load(_xmlFileName);    //加载Xml文件 

            XmlElement root = doc.DocumentElement;   //获取根节点 

            XmlNode selectNode = root.SelectSingleNode(xmlNodePath);

            if (selectNode != null)
            {

                return selectNode.InnerText;

            }

            return "无记录";

        }

        public List<string> GetTongYongTkInfo()
        {
            List<string> tmpList = new List<string>();

            tmpList.Clear();

            List<string> xPath = new List<string>();

            xPath.Add("/Root/TkInfo/工程名称[1]");

            xPath.Add("/Root/TkInfo/项目名称[1]");

            xPath.Add("/Root/TkInfo/工号[1]");

            xPath.Add("/Root/TkInfo/版本[1]");

            xPath.Add("/Root/TkInfo/阶段[1]");

            xPath.Add("/Root/TkInfo/专业[1]");

            xPath.Add("/Root/TkInfo/日期[1]");

            foreach (var item in xPath)
            {
                tmpList.Add(GetXmlElementValue(item));

            }

            return tmpList;

        }
        #endregion

        #region   与图框块名称相关的xml文件操作

      
      /// <summary>
      /// 建Root节点，Xml文件名为file_name
      /// </summary>
        public void CreateXmlFile_Root()
        {


            XDocument document = new XDocument(
                                              new XDeclaration("1.0", "utf-8", "yes"),
                                              new XElement("Root"
                                              ));
            document.Save(_xmlFileName);

        }

   
        /// <summary>
        /// 根据文件名创建记录图框信息的xml文件,如果没有Root节点会自动创建
        /// </summary>
        public void CreateXmlContent_TkName()
        {

            XDocument xdocument = null;

            XNode rootNode = null;

            try
            {

                xdocument = XDocument.Load(_xmlFileName);

                rootNode = xdocument.Root;

            }
            catch (System.Exception)
            {

                //				throw;

                CreateXmlFile_Root();

            }

            xdocument = XDocument.Load(_xmlFileName);

            //			rootNode = xdocument.Root;

            XElement xe = xdocument.Root.Element("TkNamesInfo");

            if (xe != null)
            {

                xe.Remove();

            }

            XElement Tkinfo = new XElement("TkNamesInfo",

                                           new XElement("图框块名称", "tk"),

                                           new XElement("图框块名称", "B-SZDJB-A3"),

                                           new XElement("图框块名称", "B-SZBDY-A3"),

                                           new XElement("图框块名称", "B-JTDJB-A3"),

                                           new XElement("图框块名称", "B-JTDJBBDY-A3"),

                                            new XElement("图框块名称", "B-SZDJB-A3new")

                                       );


            XElement rootElemenmt = xdocument.Root;

            rootElemenmt.Add(Tkinfo);

            xdocument.Save(_xmlFileName);


        }

        /// <summary>
        /// 获取文件内记录的图框块的名称
        /// </summary>
        /// <returns>用来存储图框块名称的List</returns>
        public List<string> Get_TkNameInfo()
        {
            List<string> tmpList = new List<string>();

            tmpList.Clear();

            string xPath = "/Root/TkNamesInfo/图框块名称";

            XmlDocument doc = new XmlDocument();

            doc.Load(_xmlFileName);    //加载Xml文件 

            XmlElement root = doc.DocumentElement;   //获取根节点 

            XmlNodeList nodeList = root.SelectNodes(xPath);

            if (nodeList != null)
            {

                foreach (XmlNode element in nodeList)
                {

                    tmpList.Add(element.InnerText);
                }

            }

            return tmpList;

        }

        /// <summary>
        /// 向Tk.xml文件加入给定的图框块名称
        /// </summary>
        /// <param name="names">需要加入Tk.xml文件的图框块名称List</param>
        public void Add_TkNamesInfo(List<string> names)
        {
            XDocument doc = XDocument.Load(_xmlFileName);

            XElement TkNamesXelement = doc.Root.Element("TkNamesInfo");

            if (TkNamesXelement != null)
            {

                //muLuXelement.RemoveAll();

                foreach (string element in names)
                {

                    XElement tmpXelement = new XElement("图框块名称", element);

                    TkNamesXelement.Add(tmpXelement);
                }

            }
            else
            {

                TkNamesXelement = new XElement("TkNamesInfo");

                foreach (string element in names)
                {

                    XElement tmpXelement = new XElement("图框块名称", element);

                    TkNamesXelement.Add(tmpXelement);
                }

                doc.Root.Add(TkNamesXelement);

            }

            doc.Save(_xmlFileName);






        }


                /// <summary>
        /// 在Tk.xml文件中移除给定的图框块名称
        /// </summary>
        /// <param name="names">需要移除的Tk.xml文件的图框块名称List</param>
        public void ReMove_TkNamesInfo(List<string> names)
        {
            XDocument doc = XDocument.Load(_xmlFileName);

            XElement TkNamesXelement = doc.Root.Element("TkNamesInfo");

            if (TkNamesXelement != null)
            {

                IEnumerable<XElement> nodes = TkNamesXelement.Elements();

                foreach (XElement e in nodes)
                {
                    string tmp = e.Value;

                    foreach (var item in names )
                    {

                        if (item==tmp)
                        {
                            e.Remove();
                        }

                    }

                }

            }
           
            doc.Save(_xmlFileName);

        }












        #endregion
        
    }
}
